//
//  HZMarketCommonView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/13.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZMarketCommonView.h"
#import "MYRadioGroup.h"
#import "HZMarketCollectionViewCell.h"

#define kMarketMargin 2
#define kMarketPadding 1

static NSString* const marketCollectionCellID = @"marketCollectionCell";

@interface HZMarketCommonView()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)MYRadioGroup     *pRadioGroup;
@property (nonatomic, strong)UICollectionView *pCollectionView;
@property (nonatomic, strong)NSMutableArray   *pIndexList;
@end

@implementation HZMarketCommonView
- (NSMutableArray *)pIndexList
{
    if (!_pIndexList) {
        _pIndexList = [NSMutableArray arrayWithCapacity:3];
    }
    return _pIndexList;
}

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self commonInit];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
#pragma mark -
#pragma mark private
- (void)commonInit
{
    _pRadioGroup = [MYRadioGroup new];
    _pRadioGroup.parent = self;
    NSArray *titleArray = [NSArray arrayWithObjects:@"", nil];
    [_pRadioGroup setRadioGroup:titleArray];
    [_pRadioGroup setHiddenSeparateLine:YES andHiddenUnderline:NO];
    [_pRadioGroup setSelectedButton:0];
    [_pRadioGroup resetAllButtonFont:[UIFont systemFontOfSize:15]];
    
    _pCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[UICollectionViewFlowLayout new]];
    _pCollectionView.dataSource = self;
    _pCollectionView.delegate = self;
    _pCollectionView.backgroundColor = [UIColor whiteColor];
    [_pCollectionView registerClass:[HZMarketCollectionViewCell class] forCellWithReuseIdentifier:marketCollectionCellID];
    
    
    [self addSubview:_pRadioGroup];
    [self addSubview:_pCollectionView];
    
    [self p_initConstraints];
}

- (void)p_initConstraints
{
    [_pRadioGroup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(43);
    }];
    [_pCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pRadioGroup.mas_bottom).offset(1);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(-1);
    }];
}
#pragma mark -
#pragma mark public
- (void)updateNewData:(NSArray *)data
{
    [self.pIndexList removeAllObjects];
    if (data) {
        for (HZOwnedStockModel *model in data) {
            if (self.pIndexList.count == 3) {
                break;
            }
            if ([model.model_Code isEqual:@"1A0001"] || [model.model_Code isEqual:@"399001"] || [model.model_Code isEqual:@"399006"]) {//1A0001/399001/399006
                [self.pIndexList addObject:model];
            }
        }
        [_pCollectionView reloadData];
    }
}

#pragma mark -
#pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.pIndexList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HZMarketCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:marketCollectionCellID forIndexPath:indexPath];
    HZOwnedStockModel *pIndexModel = _pIndexList[indexPath.row];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"code" ofType:@"plist"];
    NSArray *codeArray = [[NSArray alloc] initWithContentsOfFile:path];
    if(codeArray.count>indexPath.row){
        NSDictionary *infoDic = codeArray[indexPath.row];
        pIndexModel.model_Name = NotNilStr(infoDic[@"name"]);
    }else{
        NSDictionary *infoDic = codeArray[0];
        pIndexModel.model_Name = NotNilStr(infoDic[@"name"]);
    }
    cell.pName.text = pIndexModel.model_Name;
    cell.pPrice.text = pIndexModel.model_Price;
    cell.pZhangDie.text = pIndexModel.model_ZhangDie;
    cell.pFuDu.text = pIndexModel.model_ZhangFu;
    cell.backgroundColor = pIndexModel.model_Color;
    
    return cell;
}
#pragma mark -
#pragma mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HZOwnedStockModel *pIndexModel = _pIndexList[indexPath.row];
    [self.parent upToParentWithObjc:self withType:kHZ_jumpSingleStock_Action withParamter:pIndexModel];
}
#pragma mark -
#pragma mark UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat fCellW = (CGRectGetWidth(self.frame) - 2*kMarketMargin - 2*kMarketPadding)/3;
    CGFloat fCellH = fCellW *3/4;
    CGSize sCellSize = {fCellW, fCellH};
    
    [collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(fCellH);
    }];
    
    return sCellSize;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, kMarketMargin, 0, kMarketMargin);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 1.f;
}

#pragma mark - 
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_radioGroupClick_Action:
        {
            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
        }
            break;
            
        default:
            break;
    }
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
}

@end
